package com.ssm.controller;

import com.ssm.entity.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("shiro")
public class shiroLogin {
    private static final transient Logger log = LoggerFactory.getLogger(shiroLogin.class);

    @RequestMapping("/login")
    public String login(@RequestParam String username,@RequestParam String password){
       // System.out.println("进来！");
        log.info("哈哈哈");
        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated()) {
            System.out.println("aswer！");
            // 把用户名和密码封装为 UsernamePasswordToken 对象
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            // rememberme
            token.setRememberMe(true);
            try {
                // 执行登录.
                currentUser.login(token);
            }
            // 所有认证时异常的父类.
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
                System.out.println(ae.getMessage());
            }
        }
        return "redirect:/list.jsp";
    }
}
